package com.st.mall.product.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.st.mall.product.model.CateModel;
import com.st.mall.product.service.ICateService;


@Controller
@RequestMapping("/cate")
public class CateController {
	
	@Autowired
	private ICateService service;

	
	@RequestMapping("/insert")
	@ResponseBody
	public String insert(CateModel cateModel) {
		String result = service.insert(cateModel);
		return result;
	}
	
	@RequestMapping(value = "/selectModel",produces = "application/json;charset=utf-8")
	@ResponseBody
	public String selectModel(CateModel cateModel){
		
		List<CateModel> list = service.selectModel(cateModel);
		int result = service.selectCount(cateModel);
		Map<String,Object> map = new HashMap<>();
		map.put("data", list);
		map.put("count", result);
		return new JSONObject(map).toString();
	}
	
	@RequestMapping("/delete")
	@ResponseBody
	public int delete(String code) {
		int result = service.delete(code);
		return result;
	}
	
	@RequestMapping("/update")
	@ResponseBody
	public int update(CateModel cateModel) {
		int result = service.update(cateModel);
		return result;
	}
	
	@RequestMapping(value = "/get",produces = "application/json;charset=utf-8")
	@ResponseBody
	public String get(String code) {
		CateModel model = service.selectId(code);
		return new JSONObject(model).toString();
	}
	
	@RequestMapping(value = "/getAll",produces = "application/json;charset=utf-8")
	@ResponseBody
	public String getAll(CateModel cateModel) {
		String parentCode = cateModel.getParentCode();
		List<CateModel> list = service.selectAll(parentCode);	
		return new JSONArray(list).toString();
	}
	
}
